package cn.nju.seg.atg.GA;

public class FloatChromosome extends Chromosome {
    /** 
     * array of genes that comprise the chromosome 
     */
    protected double[] genes;

    /**
     * Parameterized constructor - 
     * Creates an double array which stores the genes 
     * @param length
     */
    public FloatChromosome(int length) {
        genes = new double[length];
    }

    /**
     * Getter for the genes array
     * @return double[]
     */
    public double[] getGenes(){
        return(genes);
    }
    
    /**
     * Get string representation of the genes.
     * @return String
     */
    public String toString(){
        return (getGenesAsString());
    }

    @Override
    public int getNumGenesInCommon(Chromosome chromosome){
        //@@ need to write some code to see how many float genes are within 10% of each other
        //if they are within 10%, they are considered to be the "same" gene
        return (genes.length);
    }

    /**
     * Return the array of genes as a string
     * @return String 
     */
    public String getGenesAsString(){
        String sGenes = "";

        for (int i = 0; i < genes.length; i++)
            sGenes += " " + genes[i] + ",";
        return (sGenes);
    }

    /**
     * Copy the genes from the given chromosome over the existing genes
     * @param Chromosome 
     */
    @Override
    public void copyChromGenes(Chromosome chromosome){
        FloatChromosome chromFloat = (FloatChromosome)chromosome;

        for (int iGene = 0; iGene < genes.length; iGene++)
            this.genes[iGene] = chromFloat.genes[iGene];
    }

    /**
     * return the gene indexed by iGene as a double
     * @param int iGene gene to get
     * @return double gene value 
     */
    public double getGene(int iGene){
        return (this.genes[iGene]);
    }

    /**
     * Set the gene indexed by iGene to the given double value
     */
    public void setGene(int iGene, double value){
        genes[iGene] = value;
    }

    /**
     * return the array of genes as a string
     * @return String
     */
    @Override
    public String getGenesAsStr(){
        String sGenes = "";
        for (int i = 0; i < genes.length; i++)
            sGenes += this.genes[i] + ",";
        return (sGenes);
    }
}
